package cn.jhz.learn.blog.test.impl;

import java.util.Optional;

import javax.validation.Valid;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import cn.jhz.learn.blog.component.RequestHolder;
import cn.jhz.learn.blog.service.core.SubfieldService;
import cn.jhz.learn.blog.dao.BlogSubfieldMapper;
import cn.jhz.learn.blog.po.BlogSubfield;

@Validated
@Service("subfieldService")
public class SubfieldServiceImpl implements SubfieldService {
	
	private final BlogSubfieldMapper subfieldMapper;
	
	public SubfieldServiceImpl(BlogSubfieldMapper subfieldMapper) {
		this.subfieldMapper = subfieldMapper;
	}
	
	@Override
	public Optional<BlogSubfield> addSubfield(BlogSubfield newSubfield) {
		return add(Optional.ofNullable(newSubfield).orElseThrow(ExceptionConstant.argumentSubfieldIsNull));
	}
	@Override
	public void removeSubfieldById(Integer subfieldId) {
		/**
		 * 	判断分支状态
		 * 	判断分支是否属于当前用户
		 * 	判断分支是否还有内容
		 */
	}

	@Override
	public Optional<BlogSubfield> modifySubfield(BlogSubfield subfield) {
		return modify(Optional.ofNullable(subfield).orElseThrow(ExceptionConstant.argumentSubfieldIsNull));
	}
	
	@Override
	public Optional<BlogSubfield> findSubfieldById(Integer subfieldId) {
		
		return null;
	}
	
	@Override
	public boolean checkArticleSubfieldIsExistById(Integer subfieldId) {
		return checkSubfieldIsExistById(subfieldId, "blog_article_subfield");
	}
	@Override
	public boolean checkPhotoSubfieldIsExistById(Integer subfieldId) {
		return checkSubfieldIsExistById(subfieldId, "blog_photo_subfield");
	}
	
	public boolean checkSubfieldIsExistById(Integer subfieldId, String tableName) {
		BlogSubfield subfield = subfieldMapper.selectByPrimaryKey(subfieldId, tableName);

		if(subfield == null)
			return false;
		else if(subfield.getBlogId().equals(RequestHolder.getCurrentUser().getId()) || subfield.getStatus() != (byte) 1)
			return false;
		else
			return true;
	}

	private Optional<BlogSubfield> add(@Valid BlogSubfield newBlogSubfield){
		return null;
	}
	
	private Optional<BlogSubfield> modify(@Valid BlogSubfield subfield){
		return null;
	}
}
